package com.tools.controller;

import com.tools.constants.Constant;
import com.tools.constants.BaseContext;
import com.tools.constants.DatabaseTypeNames;
import com.tools.model.DataBaseModel;
import com.tools.utils.AlertUtil;
import com.tools.utils.DBUtil;
import com.tools.utils.SqlLiteUtil;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.TextField;
import org.apache.log4j.Logger;

import java.net.URL;
import java.util.ResourceBundle;

/**
 * ConnectController
 *
 * @author < a href=" ">ZhangYang</ a>
 * @version v1.0.01
 * @date 2018-07-25 16:55
 */
public class ConnectController extends BaseController {
  private static final Logger LOG = Logger.getLogger(ConnectController.class);

  @FXML
  private TextField connName;
  /** 连接URL */
  @FXML
  private TextField connURL;
  /** 监听端口号 */
  @FXML
  private TextField listenPort;
  /** 数据库名称 */
  @FXML
  private TextField dbName;
  /** 数据库用户名 */
  @FXML
  private TextField userName;
  /** 数据库用户密码 */
  @FXML
  private TextField userPwd;
  /** 数据库类型 */
  @FXML
  private ComboBox<String> dbType;
  /** 数据库编码格式 */
  @FXML
  private ComboBox<String> encoding;
  /** 测试连接 */
  @FXML
  private Button testConn;
  /** 取消 */
  @FXML
  private Button quitConn;
  /** 保存 */
  @FXML
  private Button saveConn;


  @Override
  public void initialize(URL location, ResourceBundle resources) {
    saveConn.setDisable(true);
    /** 初始化下拉框 **/
    dbType.setEditable(false);
    dbType.getItems().addAll(DatabaseTypeNames.dbTypeNames());
    dbType.setValue(Constant.MYSQL);

    encoding.setEditable(false);
    encoding.getItems().addAll("utf8", "utf16", "utf32", "utf8mb4", "gb2312", "gbk", "ascii");
    encoding.setValue("utf8");

    testConn.widthProperty().addListener(w -> {
      double cw = testConn.getLayoutX() + testConn.getWidth() + 20;
      saveConn.setLayoutX(cw);
    });
    saveConn.widthProperty().addListener(w -> {
      double cw = saveConn.getLayoutX() + saveConn.getWidth() + 20;
      quitConn.setLayoutX(cw);
    });
  }


  /**
   * 保存连接
   *
   * @param event
   */
  public void saveConnAction(ActionEvent event) {
    DataBaseModel dataBaseModel = getDataBaseModel();
    if (null == dataBaseModel) {
      return;
    }
    try {
      SqlLiteUtil.saveDatabaseConfig(dataBaseModel.getConnName(), dataBaseModel);
      getDialogStage().close();
      AlertUtil.showInfoAlert("数据库【"+dataBaseModel.getConnName()+"】添加成功！");
      DataBaseController dataBaseController = (DataBaseController) BaseContext.controllerMap.get("dataBaseController");
      if(null!=dataBaseController){
        dataBaseController.loadTVDataBase();
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
  }

  /**
   * 测试连接
   *
   * @param event
   */
  public void testConnAction(ActionEvent event) {
    DataBaseModel config = getDataBaseModel();
    if (config == null) {
      saveConn.setDisable(true);
      return;
    }
    try {
      DBUtil.getConnection(config);
      AlertUtil.showInfoAlert("连接成功!");
      saveConn.setDisable(false);
    } catch (Exception e) {
      saveConn.setDisable(true);
      AlertUtil.showErrorAlert("连接失败" + e.getMessage());
    }
  }

  /**
   * 关闭当前窗口
   *
   * @param event
   */
  public void quitConnAction(ActionEvent event) {
    closeDialogStage();
  }

  public DataBaseModel getDataBaseModel() {
    String connNameText = connName.getText().trim();
    String connURLText = connURL.getText().trim();
    String listenPortText = listenPort.getText().trim();
    String dbNameText = dbName.getText().trim();
    String userNameText = userName.getText().trim();
    String userPwdText = userPwd.getText().trim();
    String dbTypeText = dbType.getValue();
    String encodingText = encoding.getValue();
    boolean isEmpty = validata(
      connNameText, connURLText, listenPortText, dbNameText, userNameText, dbTypeText, encodingText);
    if (isEmpty) {
      DataBaseModel config = new DataBaseModel(
        connNameText, connURLText, listenPortText, dbNameText, userNameText, userPwdText, dbTypeText, encodingText);
      return config;
    } else {
      AlertUtil.showWarnAlert("除了密码以外所有属性都为必需填与选择");
      return null;
    }

  }

  public boolean validata(String... str) {
    for (String string : str) {
      if (string == null || "".equals(string)) {
        return false;
      }
    }
    return true;
  }



  /** get set方法 **/
  public DataBaseController getDataBaseController() {
    return (DataBaseController)BaseContext.controllerMap.get("dataBaseController");
  }

  public TextField getConnName() {
    return connName;
  }

  public void setConnName(TextField connName) {
    this.connName = connName;
  }

  public TextField getConnURL() {
    return connURL;
  }

  public void setConnURL(TextField connURL) {
    this.connURL = connURL;
  }

  public TextField getListenPort() {
    return listenPort;
  }

  public void setListenPort(TextField listenPort) {
    this.listenPort = listenPort;
  }

  public TextField getDbName() {
    return dbName;
  }

  public void setDbName(TextField dbName) {
    this.dbName = dbName;
  }

  public TextField getUserName() {
    return userName;
  }

  public void setUserName(TextField userName) {
    this.userName = userName;
  }

  public TextField getUserPwd() {
    return userPwd;
  }

  public void setUserPwd(TextField userPwd) {
    this.userPwd = userPwd;
  }

  public ComboBox<String> getDbType() {
    return dbType;
  }

  public void setDbType(ComboBox<String> dbType) {
    this.dbType = dbType;
  }

  public ComboBox<String> getEncoding() {
    return encoding;
  }

  public void setEncoding(ComboBox<String> encoding) {
    this.encoding = encoding;
  }

  public Button getTestConn() {
    return testConn;
  }

  public void setTestConn(Button testConn) {
    this.testConn = testConn;
  }

  public Button getQuitConn() {
    return quitConn;
  }

  public void setQuitConn(Button quitConn) {
    this.quitConn = quitConn;
  }

  public Button getSaveConn() {
    return saveConn;
  }

  public void setSaveConn(Button saveConn) {
    this.saveConn = saveConn;
  }
}
